package com.xxx.system.controller;


import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.system.annotation.PreAuthorize;
import com.xxx.system.domain.Permission;
import com.xxx.system.query.PermissionQuery;
import com.xxx.system.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //默认返回json格式，前后端分离
@RequestMapping("/permission")
@Api(tags = "权限接口类")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    //所有controller的接口  都是Restful风格
//查询所有
//localhost:8080/permission  ---Get请求方式
    @GetMapping
    @ApiOperation(value = "查询所有权限")
    @PreAuthorize(name = "查询所有权限666",sn = "permission:getAll",descs = "查询所有权限")//
    public R<List<Permission>> findAll() {
        List<Permission> list = permissionService.findAll();
        return R.ok(list);
    }

    //根据id查询单个Permission
//localhost:8080/permission/{id}  ---Get请求方式
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个权限")
    public R<Permission> findOne(@PathVariable("id") Long id) {
        Permission permission = permissionService.findOne(id);
        return R.ok(permission);
    }

    //新增 或者 修改
//前端：Put：http://localhost:8080/permission/  json对象 - 请求体
//@RequestBody Permission sysUse 使用Permission对象去接受请求体中的数据，要求属性名一致
    @PutMapping
    @ApiOperation(value = "新增or修改权限")
    public R<Void> addOrUpdate(@RequestBody Permission permission) {
        try {
            //获取请求参数的id
            Long id = permission.getId();
            if (id == null) {//新增没有传递id
                permissionService.addPermission(permission);
            } else {
                permissionService.updatePermission(permission);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    //删除
//前端：Delete：http://localhost:8080/permission/2
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个权限")
    public R<Void> deleteById(@PathVariable("id") Long id) {
        try {
            permissionService.deteleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    //分页接口+高级查询(根据条件查询后也需要分页展示)
    @PostMapping
    @ApiOperation(value = "高级查询&分页")
    public PageInfo<Permission> queryPage(@RequestBody PermissionQuery permissionQuery) {
        List<Permission> permissionList = permissionService.queryPage(permissionQuery);
        //返回的是封装好的分页(总数,list)
        return new PageInfo<Permission>(permissionList);
    }

    //批量删除，接收前端id数组
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> deletePermissionByIds(@RequestBody Long[] ids) {
        try {
            permissionService.deletePermissionByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            //返回的json格式  {"success":false,"message":"错误信息"}
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }
}